package com.bp.hearthealth.pulse.lnapps.bp_db.bp_project

import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type


/**
 * @author: isnoam
 * @package_name: StringTypeConverter.kt
 * @description：使用Room框架,room不支持对象中直接存储集合
 */
class BpStringTypeConverter {
    var gson = Gson()
    @TypeConverter
    fun stringToObjectList(data: String?): List<String> {
        if (data == null) {
            return emptyList()
        }
        return try {
            val listType: Type = object : TypeToken<List<String?>?>() {}.type
            gson.fromJson(data, listType)
        } catch (e:Exception){
            emptyList()
        }
    }

    @TypeConverter
    fun objectListToString(someObjects: List<String?>?): String {
        return gson.toJson(someObjects)
    }
}
